package mt.client;

import java.util.List;



import mt.Order;


/**
 * ListUpdater will permit to the incoming messages from the Server to update the JTables and  Lists in the Graphic Interface.
 * Purchase Orders are added in the Purchase Order JTableModel.
 * Sell Orders are added in the Sell Order JTableModel.
 * Satisfied orders of the current client are added in History.
 * ListUpdater is a Singleton Class.
 * @author tom93d
 *
 */
public final class ListUpdater {
	
	/*
	 * Singleton class
	 */

	private List<Order> sellList, purchaseList;
	private JTableModel history;
	private JTableModel historySellList ;
	private JTableModel historyPurchaseList;
	private String nickName;

	private static ListUpdater instance = null;
	
	private ListUpdater() {
		
	}

	/**
	 * This method is the class constructor for the singleton Object ListUpdater.
	 * @param sellList
	 * @param purchaseList
	 * @param history
	 * @param nickName
	 * @return The only existing instance of ListUpdater.
	 */
	public static ListUpdater getInstance(List<Order> sellList, List<Order> purchaseList, JTableModel history,JTableModel historySellList,JTableModel historyPurchaseList, String nickName) {
		if(instance == null) {
			instance = new ListUpdater();
			instance.purchaseList = purchaseList;
			instance.sellList = sellList;
			instance.history = history;
			instance.historySellList = historySellList;
			instance.historyPurchaseList=historyPurchaseList;
			instance.nickName = nickName;
		}
		return instance;
	}
	
	List<Order> getSellList() {
		return this.sellList;
	}
	
	List<Order> getPurchaseList() {
		return this.purchaseList;
	}
	
	/**
	 * updateLists method will permit to the incoming messages from the Server to update the JTables and  Lists in the Graphic Interface.
	 * Purchase Orders are added in the Purchase Order JTableModel.
	 * Sell Orders are added in the Sell Order JTableModel.
	 * Satisfied orders of the current client are added in History.
	 * @param order
	 */
	void updateLists(Order order) {
		
		
		java.util.Iterator<Order> i;
		Order o;
		System.out.println("no update lists - antes do while");
		if(order != null) {
			if(order.isSellOrder()) {
				
				i = purchaseList.iterator();
			
				while(i.hasNext()) {
					System.out.println("no update lists - inicio do while");
					o = i.next();
					if(o.getStock().equals(order.getStock()) && o.getPricePerUnit() == order.getPricePerUnit()) {
						
						if(o.getNumberOfUnits() == order.getNumberOfUnits()) {
						
							//updateHistory(o,order);
							updateHistory(order,o);
							purchaseList.remove(o);
							historyPurchaseList.updateJTable();
							System.out.println("historyPurchaseList com qtd iguais" + historyPurchaseList.getData().toArray());
							return;
						} else if(o.getNumberOfUnits() > order.getNumberOfUnits()) {
					
							o.setNumberOfUnits(o.getNumberOfUnits()-order.getNumberOfUnits());
							updateHistory(order,o);
							//updateHistory(o,order);
							historyPurchaseList.updateJTable();
							System.out.println("historyPurchaseList is SellOrder com BUY>SELL" + historyPurchaseList.getData().toArray());
							return;
						} else {
					
							/*
							 * if sellorder is bigger 
							 */
						
							order.setNumberOfUnits(order.getNumberOfUnits()-o.getNumberOfUnits());
							
							updateHistory(o,order);
							//
							purchaseList.remove(o);
							historyPurchaseList.updateJTable();
							System.out.println("historyPurchaseList SELL>BUY" + historyPurchaseList.getData().toArray());
						}
			
					
					}
		
				}
				if (!sellList.contains(order)){
				sellList.add(order);
				}
				
			//	printOrders(sellList);
		
				
		
				historySellList.updateJTable();
	
			
			} else { //order.isBuyOrder()
				
				i = sellList.iterator();
			
				while(i.hasNext()) {
					System.out.println("no update lists - inicio do while");
					o = i.next();
					if(o.getStock().equals(order.getStock()) && o.getPricePerUnit() == order.getPricePerUnit()) {
						if(o.getNumberOfUnits() == order.getNumberOfUnits()) {
							
							//updateHistory(o,order);
							updateHistory(order,o);
							sellList.remove(o);
							historySellList.updateJTable();
							System.out.println("historySellList com qtd iguais " + historySellList.getData().toArray());
							return;
						} else if(o.getNumberOfUnits() > order.getNumberOfUnits()) {
							
							o.setNumberOfUnits(o.getNumberOfUnits()-order.getNumberOfUnits());
							updateHistory(order,o);
							historySellList.updateJTable();
							System.out.println("historySellList isBuy com SELL>BUY" + historyPurchaseList.getData().toString());
							return;
						} else {
							/*
							 * if buyorder is bigger 
							 */
						
							//order.setNumberOfUnits(order.getNumberOfUnits()-o.getNumberOfUnits());
							o.setNumberOfUnits(order.getNumberOfUnits()-o.getNumberOfUnits());
							updateHistory(o,order);
							sellList.remove(o);
							historySellList.updateJTable();
							System.out.println("historySellList BUY > SELL" + historySellList.getData().toArray());
						}
					}
				}
				if(!purchaseList.contains(order)){
				purchaseList.add(order);
				}
				historyPurchaseList.updateJTable();
				
			}
		
		}
	}
	
	/**
	 * UpdateHistory is called to modify the History JTableModel,
	 * It will modify the table only if the order client is the current client.
	 * @param o
	 */
	private void updateHistory(Order o,Order order) {
		System.out.println("entrou no updateHistory");
		if( (order.getNickname().equals(this.nickName)) || (o.getNickname().equals(this.nickName)) ) {
			history.addEntry(o);
	
		}
	}
	
//	private void printOrders(List<Order> sellList ){
//		java.util.Iterator<Order> i;
//		Order o;
//		i = sellList.iterator();
//		
//		while(i.hasNext()) {
//			o = i.next();
//			
//			System.err.println("PRINT_ORDERS Nick: "+o.getNickname()+" SOTCK: "+ o.getStock());
//	}
//	}

	
}
